import axios from 'axios';

//分页列表接口
const URI_DATAGRID = "/security/api/account/datagrid";
//删除接口
const URL_REMOVE = "/security/api/account/remove";
//校验账户是否唯一
const URI_ADD_VALIDATE = "/security/api/account/validateAccountAdd";
//添加账号
const URI_ADD_SUBMIT = "/security/api/account/add";
//校验账户是否唯一
const URI_EDIT_VALIDATE = "/security/api/account/validateAccountEdit";
//编辑账号
const URI_EDIT_SUBMIT = "/security/api/account/edit";
//加载角色信息
const URI_ROLES = "/security/api/account/roleData";
//关联角色
const URI_ROLE_SUBMIT = "/security/api/account/role";

export const datagrid = (params, callback = function(res) {}) => {
	axios.post(URI_DATAGRID, params).then((response) => {
        callback(response.data);
    });
}

export const remove = (ids, callback = function(res) {}) => {
	axios.post(URL_REMOVE, { ids }).then((response) => {
        callback(response.data);
    });
}

export const validateForAdd = (account, host, callback = function(isOk) {}) => {
	axios.post(URI_ADD_VALIDATE, { account, host }).then((response) => {
		const res = response.data;
		if(res.success) {
        	callback(res.data);
        } else {
        	callback(false);
        }
    });
}

export const add = (params, callback = function(res) {}) => {
	axios.post(URI_ADD_SUBMIT, params).then((response) => {
        callback(response.data);
    });
}

export const validateForEdit = (id, account, host, callback = function(isOk) {}) => {
	axios.post(URI_EDIT_VALIDATE, { id, account, host }).then((response) => {
        const res = response.data;
		if(res.success) {
        	callback(res.data);
        } else {
        	callback(false);
        }
    });
}

export const update = (params, callback = function(res) {}) => {
	axios.post(URI_EDIT_SUBMIT, params).then((response) => {
        callback(response.data);
    });
}

export const roles = (id, callback = function(res) {}) => {
	axios.post(URI_ROLES, { id }).then((response) => {
        callback(response.data);
    });
}

export const setRoles = (id, roleIds, callback = function(res) {}) => {
	axios.post(URI_ROLE_SUBMIT, { id, roleIds }).then((response) => {
        callback(response.data);
    });
}

export default {
	datagrid,
	remove,
	validateForAdd,
	add,
	validateForEdit,
	update,
	roles,
	setRoles
}